/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package validator;

import java.awt.Color;
import java.awt.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;

/**
 *
 * @author Administrator
 */
public class JStringValidator extends JLabel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private boolean _checkValidString = true;
    private boolean _checkLength   = true;

    private int _maxLength = 10;
    private int _minLength = 4;
    
    private Color _Verror = Color.red;
    private Color _Oerror = new Color(255,204,204);
    private Color _success = Color.white;

    public JStringValidator() {
        super();
    }

    public void setCheckMode(boolean checkValidString, boolean checkLength)
    {
        this._checkValidString = checkValidString;
        this._checkLength  	   = checkLength;
    }
    
    
	public Color get_Verror() {
		return _Verror;
	}

	public void set_Verror(Color _Verror) {
		this._Verror = _Verror;
	}

	public Color get_Oerror() {
		return _Oerror;
	}

	public void set_Oerror(Color _Oerror) {
		this._Oerror = _Oerror;
	}

	public Color get_success() {
		return _success;
	}

	public void set_success(Color _success) {
		this._success = _success;
	}

	public int getMaxLength() {
        return _maxLength;
    }

    public int getMinLength() {
        return _minLength;
    }
    
    public void setLength(int minLength, int maxLength){
    	this._maxLength = maxLength;
    	this._minLength = minLength;
    }

    private boolean checkStringValid()
    {
        Component obj = this.getLabelFor();
        boolean isValid = false;

        Pattern patt = Pattern.compile(ValidatorConst.STRING_VALID_PATTERN);
        Matcher matcher;

        if(obj instanceof JTextField)
        {
        	JTextField tf = (JTextField)obj;
        	matcher = patt.matcher(tf.getText());
	        if(matcher.matches())
	        {        	
	        	isValid = true;
	        }
	        else
	        {
	        	tf.grabFocus();
	        }
        }

        else if(obj instanceof JTextPane)
        {
        	JTextPane tp = (JTextPane)obj;
            matcher = patt.matcher(tp.getText());
            if(matcher.matches())
            {          		
            	isValid = true;
            }
            else
            {
            	tp.grabFocus();
            }
        }

        else if(obj instanceof JTextArea)
        {
        	JTextArea ta = (JTextArea)obj;
            matcher = patt.matcher(ta.getText());
            if(matcher.matches())
            {
            	ta.grabFocus();
                    isValid = true;
            }
            else
            {
            	ta.grabFocus();
            }
        }
        
        else if(obj instanceof JPasswordField)
        {
        	JPasswordField passField = (JPasswordField)obj;
            matcher = patt.matcher(String.valueOf(passField.getPassword()));
            if(matcher.matches())
            {
            	passField.grabFocus();
                    isValid = true;
            }
            else
            {
            	passField.grabFocus();
            }
        }

        if(!isValid)
        {
        	this.setText(ValidatorConst.VALIDATOR_STRING_INVALID_MESSAGE);
        	this.setForeground(_Verror);
        	obj.setBackground(_Oerror);
        }
        else
        {
        	this.setText("");
        	obj.setBackground(_success);
        }
        this.setVisible(!isValid);
        return isValid;
    }

    private boolean checkMaxLength()
    {
        Component obj = this.getLabelFor();
            boolean isValid = true;

            if(obj instanceof JTextField)
            {
                    if(((JTextField)obj).getText().length() > this._maxLength)
                    {
                            isValid = false;
                            ((JTextField)obj).grabFocus();
                    }
            }
            else if(obj instanceof JTextArea)
            {
                    if(((JTextArea)obj).getText().length() > this._maxLength)
                    {
                            isValid = false;
                            ((JTextArea)obj).grabFocus();
                    }
            }
            else if(obj instanceof JTextPane)
            {
                    if(((JTextPane)obj).getText().length() > this._maxLength)
                    {
                            isValid = false;
                            ((JTextPane)obj).grabFocus();
                    }
            }
            else if(obj instanceof JPasswordField)
            {
                    if(String.valueOf(((JPasswordField)obj).getPassword()).length() > this._maxLength)
                    {
                            isValid = false;
                            ((JPasswordField)obj).grabFocus();
                    }
            }
            if(!isValid)
            {
            	this.setText(ValidatorConst.VALIDATOR_STRING_MAX_LENGTH_MESSAGE + this._maxLength);
            	this.setForeground(_Verror);
            	obj.setBackground(_Oerror);
            }
            else
            {
            	this.setText("");
            	obj.setBackground(_success);
            }
            this.setVisible(!isValid);
            return isValid;
    }

    private boolean checkMinLength()
    {
        Component obj = this.getLabelFor();
            boolean isValid = true;

            if(obj instanceof JTextField)
            {
                    if(((JTextField)obj).getText().length() < this._minLength)
                    {
                    	((JTextField)obj).grabFocus();
                            isValid = false;
                    }
            }
            else if(obj instanceof JTextArea)
            {
                    if(((JTextArea)obj).getText().length() < this._minLength)
                    {
                    		((JTextArea)obj).grabFocus();
                            isValid = false;
                    }
            }
            else if(obj instanceof JTextPane)
            {
                    if(((JTextPane)obj).getText().length() < this._minLength)
                    {
                    		((JTextPane)obj).grabFocus();
                            isValid = false;
                    }
            }
            else if(obj instanceof JPasswordField)
            {
                    if(String.valueOf(((JPasswordField)obj).getPassword()).length() < this._minLength)
                    {
                            isValid = false;
                            ((JPasswordField)obj).grabFocus();
                    }
            }

            if(!isValid)
            {
            	this.setText(ValidatorConst.VALIDATOR_STRING_MIN_LENGTH_MESSAGE + this._minLength);
               	this.setForeground(_Verror);
            	obj.setBackground(_Oerror);
            }
            else
            {
            	this.setText("");
            	obj.setBackground(_success);
            }
            this.setVisible(!isValid);
            return isValid;
    }

    public boolean check()
    {
        boolean isValid = true;

        if(this._checkValidString)
        {
            isValid = isValid & this.checkStringValid();
            if(!isValid){
            	return isValid;
            }
        }
        if(this._checkLength)
        {
        	isValid = isValid & this.checkMaxLength();
        	if(!isValid){
        		return isValid;
        	}
        	
            isValid = isValid & this.checkMinLength();
            if(!isValid){
            	return isValid;
            }
        }
        return isValid;
    }
}
